package com.why.controller;

import com.alibaba.fastjson.JSON;
import com.why.pojo.User;
import com.why.pojo.UserAddress;
import com.why.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
public class UserAddressController {

    @Autowired
    private UserAddressService addressService;

    @Autowired
    private RedisTemplate redisTemplate;

    //查询用户地址的所有的信息
    @RequestMapping(value = "/userAddress", produces = "application/json;charset=utf-8")
    public Object userAddress(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("cookieUser")) {
                HashMap<String, Object> redisMap = (HashMap<String, Object>) redisTemplate.opsForValue().get(cookie.getValue());
                User user = (User) redisMap.get("user");
                List<UserAddress> userAddress = addressService.selectAddressByUserId(user.getId());
                return JSON.toJSONStringWithDateFormat(userAddress,"yyyy-MM-dd");
            }
        }
        return null;
    }

    //新增地址
    @RequestMapping("/addUserAddress")
    public String addUserAddress(UserAddress userAddress, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("cookieUser")) {
                HashMap<String, Object> redisMap = (HashMap<String, Object>) redisTemplate.opsForValue().get(cookie.getValue());
                User user = (User) redisMap.get("user");
                userAddress.setUserid(user.getId());
                if (addressService.insertSelective(userAddress)) {
                    return "success";
                }
            }
        }
        return "failed";
    }

    //删除地址
    @RequestMapping("/deleteAddress")
    public String deleteAddress(Integer id) {
        if (addressService.deleteByPrimaryKey(id)) {
            return "success";
        }
        return "failed";
    }

    //修改地址
    @RequestMapping("/updateAddress")
    public String updateAddress(UserAddress userAddress) {
        if (addressService.updateByPrimaryKeySelective(userAddress)) {
            return "success";
        }
        return "failed";
    }

    //根据id显示地址
    @RequestMapping(value = "/userAddressById",produces = "application/json;charset=utf-8")
    public UserAddress userAddressById(Integer id){
        return addressService.selectByPrimaryKey(id);
    }

    //修改默认地址
    @RequestMapping("/updateIsDefault")
    public String updateIsDefault(Integer id,HttpServletRequest request){
        User user = userData(request);
        UserAddress userAddress = new UserAddress();
        userAddress.setId(id);
        userAddress.setIsdefault(1);
        userAddress.setUserid(user.getId());
        if (addressService.updateAllIsDefault(userAddress)){
            return "success";
        }
        return "failed";
    }

    //获取用户信息，由于本类调用
    public User userData(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        //redis
        User user = null;
        for(Cookie cookie : cookies){
            if (cookie.getName().equals("cookieUser")){
                HashMap<String,Object> map = (HashMap<String, Object>) redisTemplate.opsForValue().get(cookie.getValue());
                user = (User) map.get("user");
            }
        }
        return user;
    }

}
